<%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2022/11/30
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<ul id="treeDemo" class="ztree"></ul>
<script>
    var setting = {
        async:{
            enable:true,
            url:domain+"/Menu/getAllMenu"
        },check:{
            enable:true
        },view : {
            addHoverDom: function(treeId, treeNode){
                var aObj = $("#" + treeNode.tId + "_a");
                if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
                var s = '<span id="btnGroup'+treeNode.tId+'">';
                if ( treeNode.level == 0 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="insert('+treeNode.id+')" href="#" >添加</a>';
                } else if ( treeNode.level == 1 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="update('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                    if (treeNode.children == undefined) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteById('+treeNode.id+')" href="#" >删除</a>';
                    }
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="insert('+treeNode.id+')" href="#" >添加</a>';
                } else if ( treeNode.level == 2 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="update('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteById('+treeNode.id+')" href="#">删除</a>';
                }
                s += '</span>';
                aObj.after(s);
            },
            removeHoverDom: function(treeId, treeNode){
                $("#btnGroup"+treeNode.tId).remove();
            }
        }
    };

    $(function(){
        $.fn.zTree.init($("#treeDemo"), setting);
    });

    //增加事件
    function insert(id) {
        BootstrapDialog.show({
            title: '添加信息',//标题
            message:$('<div></div>').load("/Menu/InsertById"),
            size:BootstrapDialog.SIZE_WIDE,
            buttons: [{
                label: '添加',
                action: function (dialog) {
                    $.getJSON("/Menu/InsertMenu",$("#insertMenu").serialize()+"&pid="+id, function (data) {
                        if(data>=1){
                            toastr.success("增加成功");
                            dialog.close();
                            $.fn.zTree.init($("#treeDemo"), setting);
                        }else{
                            toastr.success("增加失败");
                        }
                    });
                }
            }, {
                label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            }]
        });
    }

    //修改事件
    function update(ids) {
        BootstrapDialog.show({
            title: '修改',//标题
            message:$('<div></div>').load("/Menu/UpdateById?id="+ids),
            size:BootstrapDialog.SIZE_WIDE,
            buttons: [{
                label: '确定',
                action: function (dialog) {
                    $.getJSON("/Menu/UpdateMenu",$("#UpdateMenu").serialize(), function (data) {
                        if(data){
                            toastr.success("更新成功");
                            dialog.close();
                            $.fn.zTree.init($("#treeDemo"), setting);
                        }else{
                            toastr.error("更新失败");
                        }
                    })
                }
            }, {
                label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            }]
        });
    }

    //删除事件
    function deleteById(pid){
        if (confirm("真的要删掉我嘛＞︿＜"))
            $.getJSON("/Menu/DeleteMenu", {id: pid}, function (data) {
                if (data) {
                    toastr.success("NICE!(❁´◡`❁)");
                    //刷新页面
                    $.fn.zTree.init($("#treeDemo"), setting);
                } else {
                    toastr.error("出大事了,删不掉了o(TヘTo)");
                }
            })
    }
</script>
</body>
</html>
